/**
 * Created by CJB on 2017-02-08 15:30:04.
 */


//左边树的高度,适合所有页面
function autoZTreeHeight(){
    $("#treeDiv").css("height",getWindowHeight()-$('.grid-toolbar').height() -$('.grid-toolbar').height()- 10);
    window.onresize = function(){
        $("#treeDiv").css("height",getWindowHeight()-$('.grid-toolbar').height() -$('.grid-toolbar').height() - 10);
    }
}
var menu_setting = {
    check:{
        enable:false
//			chkStyle:"checkbox",
//			chkboxType:{"Y":"s","N":"s"}
    },
    data:{
        simpleData:{
            enable:true
        }
    },
    view:{
        showLine:false,
        dblClickExpand:true,
        fontCss:setFontCss_ztree
    },
    callback:{
        onClick:menuzTreeOnClick
    }
};

//添加时的菜单部门
var area_setting = {
    check: {
        enable: false
        //    chkboxType:  { "Y" : "ps", "N" : "ps" }
    },
    view: {
        showLine:false,
        dblClickExpand: true,
        fontCss:setFontCss_ztree
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
       // onClick:onDialogTreeOnClick("authorityAddList","parentId")
    }
};



var zTree,editzTree,addMenuzTree,areaEditTree;
var ids,names;
var DictionaryResult;
var loadDiv = null;
$(function () {
	loadDiv = $("#loadDiv").loading();//初始化提示操作执行中
    //自动获取树控件高度
    autoZTreeHeight();
    //初始化获取所有菜单
    initAreaTree();

    /*$("#addBtn").click(function(){
		 var selectedArray = zTree.getSelectedNodes();
	    	var ids = '';
	        for(var i=0;i<selectedArray.length;i++){
	            if(ids.length == 0){
	                ids += selectedArray[i].id;
	            }else{
	                ids += ","+selectedArray[i].id;
	            }
	        }
	        if(ids==null||ids==""||ids==undefined){
		        $("#content").load(getPath() + "area/info/add/" + 0);
	        }else{
	        	$("#content").load(getPath() + "area/info/add/" + ids);
	        }
	        
	    
    });*/
    $(".grid-list").load(getPath() + "area/info/add/" + 0);

    //去除所有控件的虚线框
    removeBlur();
});

/**
 * 初始化树
 */
function initAreaTree(){
    post(getPath() + "area/info/tree",{pid:0},
        function(treeNodes){
            treeNodes[0].open = true;
            $.fn.zTree.init($("#parentMenuTree"),area_setting,treeNodes);
            //左侧的菜单
            var nodes = deepClone(treeNodes);
            nodes.splice(0,1);
            zTree = $.fn.zTree.init($("#menuTree"),menu_setting,nodes);
        });
}

//菜单树的点击事件
function menuzTreeOnClick(event,treeId,treeNode){
    $(".grid-list").load(getPath() + "area/info/edit/" + treeNode.id);
}
/**
 * 获取指定id节点的数据
 * @param treeId 父节点
 * @param id id
 * @returns {*}
 */
function getNodeById(treeId, id){
    var zTree = $.fn.zTree.getZTreeObj(treeId);
    var targetNode = zTree.getNodesByFilter(function (node){
        return node.id == id;
    },true);

    return targetNode;
}

function getChildNodes(node){
	if(ids.indexOf(node.id)>0){
		return ;
	}else{
		if(ids.length>0){
			ids+= ","+node.id;
			names+=","+node.title;
		}else{
			ids+=node.id;
			names+=node.title;
		}
	}
	if(node.isParent){
		for(var index=0;index<node.children.length;index++){
			getChildNodes(node.children[index]);
		}
	}
}

//删除
function deleteById(){
    var selectedArray = zTree.getSelectedNodes();
    var promptWindow = null;
    if(selectedArray.length == 0){
        promptWindow = createWindow('请选择需要删除的地区');
    }else{
    	ids="";
    	names="";
    	for(var index=0;index<selectedArray.length;index++){
			getChildNodes(selectedArray[index]);
    	}
        promptWindow = createWindow('你确定要删除以下地区：'+names+'？',{
        	width:320,
        	height:180,
            cancleEnable:true,
            callBack:function(){
            	loadDiv.showLoading();
                post(getPath() + "area/info/delete"+"/"+ids ,{},
                    function(result){
                	loadDiv.hideLoading();
                        promptWindow.dialog("close");
                        initAreaTree();
                        $(".grid-list").html("");
                        //删除成功，弹出添加成功提示框，当用户点击确定后刷新页面
                        $(".grid-list").load(getPath() + "area/info/add/" + 0);
                        
                        
                        /*promptWindow = createWindow(result);
                        
                        promptWindow.dialog("open");  */
                    });
            }
        });
    }
    promptWindow.dialog('open');
}


//深度克隆
function deepClone(obj){
    var result,oClass=isClass(obj);
    //确定result的类型
    if(oClass==="Object"){
        result={};
    }else if(oClass==="Array"){
        result=[];
    }else{
        return obj;
    }
    for(key in obj){
        var copy=obj[key];
        if(isClass(copy)=="Object"){
            result[key]=arguments.callee(copy);//递归调用
        }else if(isClass(copy)=="Array"){
            result[key]=arguments.callee(copy);
        }else{
            result[key]=obj[key];
        }
    }
    return result;
}
//返回传递给他的任意对象的类
function isClass(o){
    if(o===null) return "Null";
    if(o===undefined) return "Undefined";
    return Object.prototype.toString.call(o).slice(8,-1);
}
